package com.helloworld.demo.sync;

public class SyncQModeTest {

    public static void main(String[] args) throws InterruptedException {

        SyncQModeTest syncQModeTest = new SyncQModeTest();

        syncQModeTest.startThreadA();

        Thread.sleep(100);

        syncQModeTest.startThreadB();

        Thread.sleep(100);

        syncQModeTest.startThreadC();

    }

    final Object lock = new Object();

    public void startThreadA(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(lock){
                    System.out.println("A get lock");

                    try {

                        Thread.sleep(300);
                        //lock.wait(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("A release lock");
                }
            }
        },"ThreadA").start();
    }

    public void startThreadB(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("B get lock");

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("B release lock");
                }

            }
        },"ThreadB").start();
    }

    public void startThreadC(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("C get lock");
                }
            }
        },"ThreadC").start();
    }

}
